| Conditions | 16 |
| Paths | 31 |
| Total Lines | 91 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like load-image-exif.js ➔ ... ➔ loadImage.parseExifData often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | /* |
||
| 217 | loadImage.parseExifData = function (dataView, offset, length, data, options) { |
||
| 218 | if (options.disableExif) { |
||
| 219 | return |
||
| 220 | } |
||
| 221 | var tiffOffset = offset + 10 |
||
| 222 | var littleEndian |
||
| 223 | var dirOffset |
||
| 224 | var thumbnailData |
||
| 225 | // Check for the ASCII code for "Exif" (0x45786966): |
||
| 226 | if (dataView.getUint32(offset + 4) !== 0x45786966) { |
||
| 227 | // No Exif data, might be XMP data instead |
||
| 228 | return |
||
| 229 | } |
||
| 230 | if (tiffOffset + 8 > dataView.byteLength) { |
||
| 231 | console.log('Invalid Exif data: Invalid segment size.') |
||
| 232 | return |
||
| 233 | } |
||
| 234 | // Check for the two null bytes: |
||
| 235 | if (dataView.getUint16(offset + 8) !== 0x0000) { |
||
| 236 | console.log('Invalid Exif data: Missing byte alignment offset.') |
||
| 237 | return |
||
| 238 | } |
||
| 239 | // Check the byte alignment: |
||
| 240 | switch (dataView.getUint16(tiffOffset)) { |
||
| 241 | case 0x4949: |
||
| 242 | littleEndian = true |
||
| 243 | break |
||
| 244 | case 0x4d4d: |
||
| 245 | littleEndian = false |
||
| 246 | break |
||
| 247 | default: |
||
| 248 | console.log('Invalid Exif data: Invalid byte alignment marker.') |
||
| 249 | return |
||
| 250 | } |
||
| 251 | // Check for the TIFF tag marker (0x002A): |
||
| 252 | if (dataView.getUint16(tiffOffset + 2, littleEndian) !== 0x002a) { |
||
| 253 | console.log('Invalid Exif data: Missing TIFF marker.') |
||
| 254 | return |
||
| 255 | } |
||
| 256 | // Retrieve the directory offset bytes, usually 0x00000008 or 8 decimal: |
||
| 257 | dirOffset = dataView.getUint32(tiffOffset + 4, littleEndian) |
||
| 258 | // Create the exif object to store the tags: |
||
| 259 | data.exif = new loadImage.ExifMap() |
||
| 260 | // Parse the tags of the main image directory and retrieve the |
||
| 261 | // offset to the next directory, usually the thumbnail directory: |
||
| 262 | dirOffset = loadImage.parseExifTags( |
||
| 263 | dataView, |
||
| 264 | tiffOffset, |
||
| 265 | tiffOffset + dirOffset, |
||
| 266 | littleEndian, |
||
| 267 | data |
||
| 268 | ) |
||
| 269 | if (dirOffset && !options.disableExifThumbnail) { |
||
| 270 | thumbnailData = { exif: {} } |
||
| 271 | dirOffset = loadImage.parseExifTags( |
||
| 272 | dataView, |
||
| 273 | tiffOffset, |
||
| 274 | tiffOffset + dirOffset, |
||
| 275 | littleEndian, |
||
| 276 | thumbnailData |
||
| 277 | ) |
||
| 278 | // Check for JPEG Thumbnail offset: |
||
| 279 | if (thumbnailData.exif[0x0201]) { |
||
| 280 | data.exif.Thumbnail = loadImage.getExifThumbnail( |
||
| 281 | dataView, |
||
| 282 | tiffOffset + thumbnailData.exif[0x0201], |
||
| 283 | thumbnailData.exif[0x0202] // Thumbnail data length |
||
| 284 | ) |
||
| 285 | } |
||
| 286 | } |
||
| 287 | // Check for Exif Sub IFD Pointer: |
||
| 288 | if (data.exif[0x8769] && !options.disableExifSub) { |
||
| 289 | loadImage.parseExifTags( |
||
| 290 | dataView, |
||
| 291 | tiffOffset, |
||
| 292 | tiffOffset + data.exif[0x8769], // directory offset |
||
| 293 | littleEndian, |
||
| 294 | data |
||
| 295 | ) |
||
| 296 | } |
||
| 297 | // Check for GPS Info IFD Pointer: |
||
| 298 | if (data.exif[0x8825] && !options.disableExifGps) { |
||
| 299 | loadImage.parseExifTags( |
||
| 300 | dataView, |
||
| 301 | tiffOffset, |
||
| 302 | tiffOffset + data.exif[0x8825], // directory offset |
||
| 303 | littleEndian, |
||
| 304 | data |
||
| 305 | ) |
||
| 306 | } |
||
| 307 | } |
||
| 308 | |||
| 321 |